<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\Relation;
use app\common\model\UserNotify;
use think\exception\ValidateException;
use think\facade\View;
use app\common\model\User as UserModel;
use think\response\Json;

class User extends Base
{

    public function index(): Json
    {
        $result = [
            'nickname' => $this->user->nickname,
            'code' => $this->user->code,
            'photo' => $this->user->photo,
            'is_active' => $this->user->is_active == 1 ? '普通会员' : '正式会员',
            'level' => $this->user->level ? $this->user->level->title : '',
            'mobile' => $this->user->mobile ?: '',
            'parent_code' => $this->user->parent_code ?: '',
        ];
        return json(['code' => 1, 'msg' => 'success', 'result' => $result]);
    }

    public function setting()
    {
        return View::fetch();
    }

    public function my_team()
    {

        $page = $this->request->param('page', 1);
        $map = ['uid' => $this->user->id, 'algebra' => 1];
        $list = Relation::getPageList($map, 'id,uid,new_uid,create_time', $page, 20, false, ['fresh']);
        foreach ($list['result'] as $k => $v) {
            $v->code = $v->fresh->code;
            $v->mobile = $v->fresh->mobile;
            $v->team_total = $v->fresh->team_total;
            unset($v->id);
            unset($v->uid);
            unset($v->new_uid);
            unset($v->fresh);
        }
        $team = [];
        if ($page == 1) {
            $dpn = Relation::where(['uid' => $this->user->id, 'algebra' => 1])->column('new_uid');
            $team = Relation::where('uid', $this->user->id)->count();
            $team = [
                'dpn' => count($dpn),
                'dpn_total' => UserModel::whereIn('id', $dpn)->sum('my_total'),
                'team' => $team,
                'team_total' => $this->user->team_total
            ];
        }
        return json(['code' => 1, 'msg' => 'success', 'result' => [
            'team' => $team, 'list' => $list['result'], 'pages' => $list['pages']
        ]]);
    }

    public function method_index()
    {
        $mode = [
            ['name' => '支付宝', 'type' => 'alipay'],
            ['name' => '微信', 'type' => 'wechat'],
            ['name' => '银行卡', 'type' => 'bank']
        ];
        return json(['code' => 1, 'msg' => 'success', 'result' => $mode]);
    }

    public function method_info($type)
    {
        switch ($type) {
            case "alipay":
            case "wechat":
                $name = $type . '_name';
                $image = $type . '_image';
                $result['name'] = $this->user->$name;
                $result['image'] = handleImg($this->user->$image);
                break;
            case "bank":
                $result['bank_name'] = $this->user->bank_name;
                $result['bank_open'] = $this->user->bank_open;
                $result['bank_account_name'] = $this->user->bank_account_name;
                $result['bank_number'] = $this->user->bank_number;
                $result['bank_list'] = explode(PHP_EOL, $this->system['bank_list']);
                break;
            default:
                return json(['code' => -1, 'msg' => '加载失败']);

        }
        return json(['code' => 1, 'msg' => 'success', 'result' => $result]);
    }

    public function method_save($type)
    {
        try {
            $param = $this->request->post('', '', $this->filter);

            $v = $type == 'bank' ? 'MethodBank' : 'MethodImg';
            try {
                $this->validate($param, $v);
            } catch (ValidateException $e) {
                return json(['code' => -1, 'msg' => $e->getError()]);
            }
            switch ($type) {
                case "bank":
                    break;
                default:
                    $param[$type . '_name'] = $param['name'];
                    $param[$type . '_image'] = $param['image'];
                    unset($param['name']);
                    unset($param['image']);
            }

            UserModel::update($param, ['id' => $this->user->id]);

            return json(['code' => 1, 'msg' => '已保存']);

        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }

    public function edit()
    {
        $param = $this->request->post('', '', $this->filter);
        return json(UserModel::_miniAppletEdit($param, $this->user));
    }

    public function notify($type)
    {
        $param = $this->request->param();

        if ($type == 1) {
            $count = UserNotify::where(['uid' => $this->user->id, 'is_read' => 0])->count();
            return json(['code' => 1, 'msg' => 'success', 'result' => $count]);
        } else {
            $map = ['uid' => $this->user->id, 'is_del' => 0];

            return json(UserNotify::getPageList($map, 'id,type,content,is_read,create_time', $param['page'], 20, false));
        }


    }


}

